package com.ebelter.btlibrary.btble.impl.wristband;


import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.BleMessageAnalyser;
import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.Channel;
import com.ebelter.btlibrary.btble.impl.wristband.callback.WristbandResultCallback;
import com.ebelter.btlibrary.btble.impl.wristband.model.StepResult;
import com.ebelter.btlibrary.util.ULog;

/**
 * 耳温枪数据解析器
 * Created by Sergio Pan on 2017/12/8.
 */

public class WristbandMessageAnalyser extends BleMessageAnalyser {

    private static final String TAG = "WristbandMessageAnalyser";

    private static WristbandMessageAnalyser instance = new WristbandMessageAnalyser();

    private WristbandResultCallback mResultCallback;


    private WristbandMessageAnalyser() {
//        recordSet.
    }


    public static WristbandMessageAnalyser getInstance() {
        return instance;
    }

    public void registerMeasureResultCallback(WristbandResultCallback callback) {
        this.mResultCallback = callback;
    }


    /**
     * 解密数据
     *
     * @param data
     * @return
     */
    @Override
    protected byte[] decrypt(byte[] data) {
//        ULog.i(TAG, "-------decrypt--------data = " + getArrayString(data));
        //TODO 如若是加密数据，在此做解密处理,如若非加密数据，可不重写此方法

        return data;
    }

    /**
     * 开始解析
     *
     * @param data
     * @return
     */
    @Override
    protected void analyze(Channel channel, byte[] data) {
        ULog.i(TAG, "-------analyze----before filter----channel = " + channel + ", data = " + getArrayString(data));
        //解密后
//        if (data.length < 2 || data[0] != dataHeaderReceive || data.length != (data[1] & 0xff) + 3) {
//            return;
//        }
//        ULog.i(TAG, "-------analyze---after filter-----data = " + getArrayString(data));
        //业务码

        int cmdCode = data[0] & 0xff;
        ULog.i(TAG, "-------analyze--------cmdCode = " + Integer.toHexString(cmdCode));
        switch (cmdCode) {
            case 0x1c:

                break;
            case 0xb1: // 步数/里程/卡里路
                doSteps(data);
                break;
            case 0xc7: // 脉搏测血压,高压、低压

                break;
            case 0xd1: // 开关机相关的

                break;
            case 0xe5: // 心率
                doHeartRate(data);
                break;


        }


    }

    /**
     * 处理里程
     *
     * @param data
     */
    private void doSteps(byte[] data) {

        if (data.length <= 11) {
            return;
        }

        int month = data[3]&0xff;
        int day = data[4]&0xff;
        int hour = data[5]&0xff;

        int a = get2ByteValue(data[6], data[7]);
        int b = get2ByteValue(data[8], data[9]);
        int steps = get2ByteValue(data[10], data[11]);

        ULog.i(TAG,"--------doSteps----------month = "+month+", day = " + day+ ", hour = " + hour+", a  = " + a+", b = " + b +", steps = " + steps);
        StepResult result = new StepResult();
        result.setSteps(steps);

//        if (mResultCallback != null) {
////            mResultCallback.onReceiveSteps(result);
//        }

    }

    /**
     * 处理心率
     *
     * @param data
     */
    private void doHeartRate(byte[] data) {
//        if (data.length <= 3) {
//            return;
//        }
//        int heartRate = data[3] & 0xff;
//        if (mResultCallback != null) {
////            mResultCallback.onReceiveHeartRate(heartRate);
//        }
    }


    @Override
    public void onRelease() {
        super.onRelease();


    }
}
